//
//  NewsRootViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-8.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "NewsRootViewController.h"

#import "CenterTableViewController.h"
#import "LeftTableViewController.h"

#import "WeatherViewController.h"

#import "ShareDataHandle.h"

//左抽屉宽度
#define kWidethOfDrawer 100

@interface NewsRootViewController () <jumpToCenterDelegete>

{
    CGFloat _startX;
    CGFloat _endX;
    BOOL _isLeft;
    BOOL _isRight;
    BOOL _isDayNight;
    
}

@property (nonatomic, strong)CenterTableViewController *center;
@property (nonatomic, strong)LeftTableViewController *left;
@property (nonatomic, strong)UIView *alpaView;
@property (nonatomic, retain)UIView *rightView;

@property (nonatomic, retain)UIView *dayView;

@end

@implementation NewsRootViewController

- (void)dealloc{
    
    [_center release];
    [_left release];
    [_alpaView release];
    [_rightView release];
    [_dayView release];
    
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zuo.png"] style:UIBarButtonItemStyleDone target:self action:@selector(buttonAction:)];
    
  //  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"calendar_switch_up@2x.png"] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
    
    self.view.userInteractionEnabled = YES;
    
    UIPanGestureRecognizer *pan = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)] autorelease];
    // pan.maximumNumberOfTouches = 1;
    [self.view addGestureRecognizer:pan];
    
    //设置标题
    self.navigationItem.title = @"头条";
    
    //左抽屉的代理,用来点击做抽屉cell时响应一定的事件
    self.left.delegate = self;

  //  [self.center.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:17], NSFontAttributeName ,nil]];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark 初始化方法
- (id)initWithCenter:(CenterTableViewController *)center left:(LeftTableViewController *)left
{
   //应该先移除旧的再添加
    self = [self init];
    
    _left = left;
    [self.view addSubview:left.view];
    [self addChildViewController:left];
    
    _center = center;
    _center.tableView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 108);
    [self.view addSubview:center.view];
    [self addChildViewController:center];
    
    _isLeft = NO;
    _isRight = NO;
    _isDayNight = NO;
    
    return self;
    
}

#pragma mark 右边按钮的功能
- (void)rightBarButtonAction:(UIBarButtonItem *)sender
{
    NSLog(@"执行了");
    if (!_isRight) {
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"calendar_switch_down@2x.png"]];
            
            [self addRightView];
            [self.view addSubview:self.rightView];
            _isRight = YES;
        }];
       
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"calendar_switch_up@2x.png"]];
            
            [self.rightView removeFromSuperview];
            _isRight = NO;

        }];
    }
    
    
}

//点击button打开/关闭抽屉
#pragma mark 点击button打开关闭抽屉
- (void)buttonAction:(UIBarButtonItem *)sender
{
  //  NSLog(@"点了");
    
    //打开抽屉
    if (!_isLeft) {
        
        [UIView animateWithDuration:0.5 animations:^{
            [self drawerOpenLayout];
            _isLeft = YES;
            
        } completion:nil];
        
    }else{
        
        [UIView animateWithDuration:0.5 animations:^{
            
            [self drawerCloseLayout];
            _isLeft = NO;
            
        } completion:nil];
    }
}


//拖时打开关闭抽屉
#pragma mark 拖时弹出抽屉
- (void)panAction:(UIPanGestureRecognizer *)sender
{
 
    CGPoint translation = [sender translationInView:sender.view];
    
    if (translation.x > 0) {
        
        if (!_isLeft) {
            //抽屉打开
         //   NSLog(@"kai时了");
            
            [UIView animateWithDuration:0.5 animations:^{
                
                [self drawerOpenLayout];
                _isLeft = YES;
                
            } completion:nil];
        }
        //    }else{
        //        //关闭抽屉
        //         if (sender.state == UIGestureRecognizerStateBegan) {
        //
        //             [UIView animateWithDuration:0.5 animations:^{
        //                 [self drawerCloseLayout];
        //                 _isLeft = NO;
        //             }];
        //         }
        //     }
        
    }
       //添加动画的抽屉
    /*
    //距离
    CGPoint translation = [sender translationInView:self.view];
    if (!_isLeft) {
       
        if (translation.x > 0) {
            
            //起始位置
            if (sender.state == UIGestureRecognizerStateBegan) {
                _startX = 0;
            }

            
        _endX = _startX + translation.x;
        
        if (sender.state == UIGestureRecognizerStateEnded) {
            
            _endX = translation.x + _startX;
            
            if (_endX >= 50) {
                _endX = kWidethOfDrawer;
                
                _isLeft = YES;
                
            }else{
                _endX = 0;
                
            }
            
//            [self addAlphaView];
//            [self.view addSubview:self.alpaView];
            
        }
        
        [UIView animateWithDuration:0.2 animations:^{
            
            self.center.view.frame = CGRectMake(_endX, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
            
            }];
            
        }
        
    }else{
        if (translation.x < 0 && translation.x > -50) {
        
        NSLog(@"%f",translation.x);
        if (sender.state == UIGestureRecognizerStateBegan) {
            _startX = kWidethOfDrawer;
        }
        
        _endX = _startX + translation.x;
            
        if (sender.state == UIGestureRecognizerStateEnded) {
            
            if (_endX > 50) {
                _endX = kWidethOfDrawer;
                
                _isLeft = NO;
            }else{
                _endX = 0;
            }
        }
        
        [UIView animateWithDuration:0.2 animations:^{
            
            self.center.view.frame = CGRectMake(_endX, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
            
        }];
            
        }
        
      //  [self drawerCloseLayout];
    }
    */
}


//点击收回抽屉
#pragma mark 点击收回抽屉
- (void)tapAction:(UITapGestureRecognizer *)sender
{
   // NSLog(@"点击执行");
    
    //if (sender.state == UIGestureRecognizerStateBegan) {
    
    if (_isLeft) {
        
      //  NSLog(@"no");
        
        [UIView animateWithDuration:0.3 animations:^{
            
            [self drawerCloseLayout];
            
            
        } completion:^(BOOL finished) {
            
            _isLeft = NO;
        }];
        
    }else{
        
        [UIView animateWithDuration:0.5 animations:^{
            [self drawerOpenLayout];
            _isLeft = YES;
        }];
      //  NSLog(@"yes");
    }
    
    
    // }
}

#pragma mark 关闭抽屉时的布局
- (void)drawerCloseLayout
{
    [self.alpaView removeFromSuperview];
    self.center.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 108);
    [self.view bringSubviewToFront:self.center.view];
    
}

#pragma mark 打开抽屉时页面的布局
- (void)drawerOpenLayout
{
    self.center.view.frame = CGRectMake(kWidethOfDrawer, 64, self.view.frame.size.width, self.view.frame.size.height);
    self.left.view.frame = CGRectMake(0, 0, kWidethOfDrawer, self.view.frame.size.height);
   
   
   //添加阴影图层
    [self addAlphaView];
    
    [self.center.view addSubview:self.alpaView];

    
    
  //  [self.view bringSubviewToFront:self.left.view];
}

#pragma mark 添加图层
- (void)addAlphaView
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;
    
    //添加一个阴影的图层
//    self.alpaView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height)] autorelease];
    
    self.alpaView = [[[UIView alloc] initWithFrame:self.center.view.bounds] autorelease];
    self.alpaView.backgroundColor = [UIColor blackColor];
    self.alpaView.alpha = 0.3;
    self.alpaView.userInteractionEnabled = YES;
    
    [self.alpaView addGestureRecognizer:tap];
}


#pragma mark 右边添加view
- (void)addRightView
{
    self.rightView = [[[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 120, 64, 120, 120)] autorelease];
    self.rightView.backgroundColor = [UIColor whiteColor];
    self.rightView.alpha = 0.95;
    
    UIButton *weatherButton = [UIButton buttonWithType:UIButtonTypeSystem];
    weatherButton.frame = CGRectMake(40, 0, 40, 40);
    [weatherButton setTitle:@"天气" forState:UIControlStateNormal];
    [weatherButton setTintColor:[UIColor blackColor]];
    [weatherButton addTarget:self action:@selector(weatherAction:) forControlEvents:UIControlEventTouchUpInside];
   // [weatherButton setImage:<#(UIImage *)#> forState:<#(UIControlState)#>]
    [self.rightView addSubview:weatherButton];
    
    UIButton *seachButton = [UIButton buttonWithType:UIButtonTypeSystem] ;
    seachButton.frame = CGRectMake(CGRectGetMinX(weatherButton.frame), CGRectGetMaxY(weatherButton.frame), CGRectGetWidth(weatherButton.frame), CGRectGetHeight(weatherButton.frame));
    [seachButton setTitle:@"搜索" forState:UIControlStateNormal];
    [seachButton setTintColor:[UIColor blackColor]];
    [seachButton addTarget:self action:@selector(seachAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightView addSubview:seachButton];
    
    UIButton *nigthButton = [UIButton buttonWithType:UIButtonTypeSystem];
    nigthButton.frame = CGRectMake(CGRectGetMinX(seachButton.frame), CGRectGetMaxY(seachButton.frame), CGRectGetWidth(seachButton.frame), CGRectGetHeight(seachButton.frame));
    [nigthButton setTitle:@"夜间" forState:UIControlStateNormal];
    [nigthButton setTintColor:[UIColor blackColor]];
    [nigthButton addTarget:self action:@selector(nightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightView addSubview:nigthButton];
    
    
}

#pragma mark 夜间模式
- (void)nightButtonAction:(UIButton *)sender
{
    /*
    self.dayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.dayView.backgroundColor = [UIColor blackColor];
    self.dayView.alpha = 0.4;

    
    if (!_isDayNight) {
        
        [self.view addSubview:self.dayView];
        _isDayNight = YES;
        
    }else{
        
        [self.dayView removeFromSuperview];
        _isDayNight = NO;
    }
   */
}

#pragma mark 天气按妞事件
- (void)weatherAction:(UIButton *)sender
{
    WeatherViewController *weather = [[[WeatherViewController alloc] init] autorelease];
    
    
    [self.navigationController presentViewController:weather animated:YES completion:nil];
    
}

#pragma mark 搜索按钮方法
- (void)seachAction:(UIButton *)sender
{
    NSLog(@"搜索中");
    
    
}


//跳转
#pragma mark 点击左cell更新数据
- (void)jumpToCenterControllerView
{
  //  NSLog(@"执行了");
    
    [UIView animateWithDuration:0.5 animations:^{
        
        
       [self drawerCloseLayout];
        
       // UIAlertView *alt = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
//        UIActivityIndicatorView *indView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//        indView.frame = CGRectMake(100, 20, 50, 50);
//        [self.center.view addSubview:indView];
//        
//        [indView startAnimating];

        
        _isLeft = NO;
        
    } completion:nil];
    
}

#pragma mark 传参的代理方法
- (void)passCidString:(NSString *)str titltString:(NSString *)titleStr
{
    self.center.passedStr = str;
    self.center.num = 0;
    
    [[ShareDataHandle shareDataHandle] parseDataWithCenterKeyString:self.center.passedStr numberStr:[NSString stringWithFormat:@"%d",self.center.num]];
    
    self.navigationItem.title = titleStr;
    
}










/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
